package villa.tools.mq.views;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PlatformUI;

import villa.tools.mq.Activator;

public class ToolBarView {
	
	private Action newAction = null;
	private Action saveAction = null;
	private Action putQMAction = null;
	private Action closeAction = null;
	private Action wrapToggleAction = null;
	private Action indentToggleAction = null;
	private Action indentAction = null;
	private Action getQMAction = null;
	private Action escapeXMLAction = null;
	private Action unescapeXMLAction = null;
	
	private static ToolBarView instancia = null;
	private IActionBars bars = null;
	
	private ToolBarView() {
		//prop = new Properties();
	}

	public static ToolBarView getInstancia(){
		if (instancia==null){
				instancia = new ToolBarView();
		}
		return instancia;
	}
	
	public void setBar(IActionBars bar1){
		bars = bar1;
	}
	
	public void init(){
		makeActions();
		contributeToActionBars();
	}
	
	public void contributeToActionBars() {
		Action selectAllAction = new Action() {
			public void run() {
				TabFolder tf = PrincipalView.getInstancia().getTabFolder();
				TabControlsP tcp = (TabControlsP) tf.getItem(tf.getSelectionIndex()).getControl();
		        TabControlsO tabControls = (TabControlsO) tcp.getItem(0).getControl();//0-Message

		        if (tabControls.styledTextOTHER.isFocusControl()) {
					tabControls.styledTextOTHER.selectAll();
				} else if (tabControls.styledTextPut.isFocusControl()) {
					tabControls.styledTextPut.selectAll();
				} else if (tabControls.styledTextGet.isFocusControl()) {
					tabControls.styledTextGet.selectAll();
				} else if (tabControls.qManagerText.isFocusControl()) {
					tabControls.qManagerText.selectAll();
				}
			}
		};
		//IActionBars bars = view.getViewSite().getActionBars();
		Action undoAction = new Action() {
			public void run() {
				TabFolder tf = PrincipalView.getInstancia().getTabFolder();
				TabControlsP tcp = (TabControlsP) tf.getItem(tf.getSelectionIndex()).getControl();
		        TabControlsO tabControls = (TabControlsO) tcp.getItem(0).getControl();//0-Message

		        if (tabControls.styledTextOTHER.isFocusControl()) {
					tabControls.styledTextOTHER.undo();
				} else if (tabControls.styledTextPut.isFocusControl()) {
					tabControls.styledTextPut.undo();
				} else if (tabControls.styledTextGet.isFocusControl()) {
					tabControls.styledTextGet.undo();
				}
			}
	    };
	    Action redoAction = new Action() {
	    	public void run() {
				TabFolder tf = PrincipalView.getInstancia().getTabFolder();
				TabControlsP tcp = (TabControlsP) tf.getItem(tf.getSelectionIndex()).getControl();
		        TabControlsO tabControls = (TabControlsO) tcp.getItem(0).getControl();//0-Message

		        if (tabControls.styledTextOTHER.isFocusControl()) {
					tabControls.styledTextOTHER.redo();
				} else if (tabControls.styledTextPut.isFocusControl()) {
					tabControls.styledTextPut.redo();
				} else if (tabControls.styledTextGet.isFocusControl()) {
					tabControls.styledTextGet.redo();
				}
			}
	    };
		
	    bars.setGlobalActionHandler(IWorkbenchActionConstants.SELECT_ALL, selectAllAction);
	    bars.setGlobalActionHandler(IWorkbenchActionConstants.UNDO, undoAction);
        bars.setGlobalActionHandler(IWorkbenchActionConstants.REDO, redoAction);
        
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}
	
	private void fillLocalPullDown(IMenuManager manager) {
		manager.add(getQMAction);
		manager.add(putQMAction);
		manager.add(new Separator());
		manager.add(indentAction);
		manager.add(escapeXMLAction);
		manager.add(unescapeXMLAction);
		manager.add(new Separator());
		manager.add(newAction);
		manager.add(closeAction);
		manager.add(new Separator());
		manager.add(indentToggleAction);
		manager.add(wrapToggleAction);
	}

	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(getQMAction);
		manager.add(putQMAction);
		manager.add(new Separator());
		manager.add(indentAction);
		manager.add(escapeXMLAction);
		manager.add(unescapeXMLAction);
		manager.add(new Separator());
		manager.add(newAction);
		manager.add(saveAction);
		manager.add(closeAction);
		manager.add(new Separator());
		manager.add(indentToggleAction);
		manager.add(wrapToggleAction);
	}
	
	private void makeActions() {
		saveAction = new Action() {
			public void run() {
				TabFolder tf = PrincipalView.getInstancia().getTabFolder();
				Actions.saveActionF(tf);
			}
		};
		saveAction.setText("Save");
		saveAction.setToolTipText("Save Current State");
		saveAction.setImageDescriptor(getImageDescriptor("saveall_edit.gif"));

		newAction = new Action() {
			public void run() {
				TabFolder tf = PrincipalView.getInstancia().getTabFolder();
				if (tf.getItemCount() == 0) {
					closeAction.setEnabled(true);
					putQMAction.setEnabled(true);
					getQMAction.setEnabled(true);
				}
				Actions.newActionF(tf);
			}
		};
		newAction.setText("New");
		newAction.setToolTipText("Create Tab");
		newAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD));

		closeAction = new Action() {
			public void run() {
				TabFolder tf = PrincipalView.getInstancia().getTabFolder();
				Actions.closeActionF(tf);
				if (tf.getItemCount() == 0) {
					closeAction.setEnabled(false);
					putQMAction.setEnabled(false);
					getQMAction.setEnabled(false);
				}
			}
		};
		closeAction.setText("Close");
		closeAction.setToolTipText("Close Tab");
		closeAction.setImageDescriptor(getImageDescriptor("delete_edit.gif"));

		putQMAction = new Action() {
			public void run() {
				TabFolder tf = PrincipalView.getInstancia().getTabFolder();
				Actions.putQMActionF(tf);
			}
			
		};

		putQMAction.setText("Put Message Queue");
		putQMAction.setToolTipText("Put Message Queue");

		putQMAction.setImageDescriptor(getImageDescriptor("resume_co.gif"));

		wrapToggleAction = new Action("Word Wrap", Action.AS_CHECK_BOX) {
			public void run() {
				boolean val = Basic.getInstancia().getWrap();
				Actions.wrapToggleActionF(!val);
			}
		};
		wrapToggleAction.setToolTipText("Word Wrap");
		wrapToggleAction.setImageDescriptor(getImageDescriptor("wrap.gif"));
		wrapToggleAction.setChecked(Basic.getInstancia().getWrap());

		indentToggleAction = new Action("Toggle Indent", Action.AS_CHECK_BOX) {
			public void run() {
				boolean val = Basic.getInstancia().getIndent();
				Actions.indentToggleActionF(!val);
			}
		};
		indentToggleAction.setToolTipText("Toggle Indent");
		indentToggleAction.setImageDescriptor(getImageDescriptor("toggleindent.gif"));
		indentToggleAction.setChecked(Basic.getInstancia().getIndent());

		
		getQMAction = new Action() {
			public void run() {
				TabFolder tf = PrincipalView.getInstancia().getTabFolder();
				Actions.getQMActionF(tf);
			}
		};
		getQMAction.setText("Get Message Queue");
		getQMAction.setToolTipText("Get Message Queue");
		getQMAction.setImageDescriptor(getImageDescriptor("getwsdl.gif"));

		escapeXMLAction = new Action() {
			public void run() {
				TabFolder tf = PrincipalView.getInstancia().getTabFolder();
				Actions.escapeXMLActionF(tf);
			}
		};
		escapeXMLAction.setText("Escape XML");
		escapeXMLAction.setToolTipText("Escape XML");
		escapeXMLAction.setImageDescriptor(getImageDescriptor("escapexml.gif"));

		unescapeXMLAction = new Action() {
			public void run() {
				TabFolder tf = PrincipalView.getInstancia().getTabFolder();
				Actions.unescapeXMLActionF(tf);
			}
		};
		unescapeXMLAction.setText("Unescape XML");
		unescapeXMLAction.setToolTipText("Unescape XML");
		unescapeXMLAction.setImageDescriptor(getImageDescriptor("unescapexml.gif"));

		indentAction = new Action() {
			public void run() {
				TabFolder tf = PrincipalView.getInstancia().getTabFolder();
				Actions.indentActionF(tf);
			}
		};
		indentAction.setText("Indent XML");
		indentAction.setToolTipText("Indent XML in Active Editor");
		indentAction.setImageDescriptor(getImageDescriptor("indent.gif"));
	}

	private ImageDescriptor getImageDescriptor(String relativePath) {
		String iconPath = "icons/";
		try {
			Activator plugin = Activator.getDefault();
			URL installURL = plugin.getDescriptor().getInstallURL();
			URL url = new URL(installURL, iconPath + relativePath);
			return ImageDescriptor.createFromURL(url);
		} catch (MalformedURLException e) {
			return ImageDescriptor.getMissingImageDescriptor();
		}
	}
	
}
